package cn.liberals.controller;

import cn.liberals.domain.Product;
import cn.liberals.domain.Store;
import cn.liberals.feign.StoreFeignClient;
import cn.liberals.service.IProductService;
import cn.liberals.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

    @Autowired
    private StoreFeignClient storeFeignClient;

    @Autowired
    private IProductService productService;

    @PostMapping("product/add")
    public AjaxResult add(@RequestBody Product product){
        try {
            productService.add(product);
            return storeFeignClient.save(Store.builder().productId(product.getId()).number(1).build());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常");
        }
    }
}
